# rename 重命名主机名，用于重置后改主机名
rename new_name

- new_name 新主机名

function rename([string]$newname){
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type . newname => $newname."
    }
    Else
    {
        rename-computer -Newname $newname -Restart
    }
}

# password 重置密码，用户必须时存在的，适用于administrator 的启用和密码重置
password name password

function password($name,$password){
    net user $name $password
    net user $name /active:yes
}

# NewFirewallRule PS脚本实现添加防火墙入站规则 接收2个参数 （支持win8以上系统，win7等使用netsh）
newfirewallrule display_name port

function NewFirewallRule($fw_rule_name,$port){
    Remove-NetFirewallRule -DisplayName $fw_rule_name
    New-NetFirewallRule -DisplayName $fw_rule_name -Action Allow -Direction Inbound -Protocol TCP -LocalPort $port 
}

# joindomain 加入域,4个参数
joindomain domain_name domain_admin admin_password new_computername  

- domain_name 域名
- domain_admin 域管理员账号
- admin_password 管理员账号密码
- new_computername  新主机名（加域需要使用不同的主机名区分，不然没法加入域）

function joindomain($domain,$username,$psword,$newname){
    echo "++++++添???加??域???++++++"
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type . domain => $domain, username => $username, password => $psword, newname => $newname."
    }
    Else
    {
        $password = ConvertTo-SecureString $psword -AsPlainText -Force
        $credential = New-Object System.Management.Automation.PSCredential($username,$password)
        # 如果newname 是原来的名字一样可能是有问题需要fix
        Add-Computer -DomainName $domain -Credential $credential -NewName $newname -Restart
        echo "结??果?为a：?o $?"
        echo " "
        sleep 2
    }
}

# unjoindomain 脱域/退出域,2个参数
 unjoindomain domain_admin admin_password

- domain_admin 域管理员账号
- admin_password 管理员账号密码

function unjoindomain([string]$username,[string]$psword){
    echo "++++++脱??域???++++++"
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type . username => $username, password => $psword."
    }
    Else
    {
        $password = ConvertTo-SecureString $psword -AsPlainText -Force
        $credential = New-Object System.Management.Automation.PSCredential($username,$password)
        Remove-Computer -UnjoinDomaincredential $credential -PassThru -Verbose -Restart -Force
        echo "结??果?为a：?o $?"
        echo " "
        sleep 2
    }
}

# rename_indomain 在域环境下重命名主机名
rename_indomain domain_admin admin_password newname

- domain_admin 域管理员账号
- admin_password 管理员账号密码
- newname 新主机名

function rename_indomain($username,$ps,[string]$newname){
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type . newname => $newname."
    }
    Else
    {
        $password = ConvertTo-SecureString $ps -AsPlainText -Force
        $credential = New-Object System.Management.Automation.PSCredential($username,$password)
        #rename-computer -LocalCredential $credential -Newname $newname 
        rename-computer -DomainCredential $credential -Newname $newname -Restart -Force
    }
}

# addgroupmember 将域用户加入administrators组 
# 加入域后，将域用户加入administrators组中
addgroupmember 域用户

function AddGroupMember($member){
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type.member => $member"
    }
    Else
    {
        Add-LocalGroupMember -Group "Administrators" -Member $member
    }
}

# rmgroupmember 将域用户从administrators组移除 
# 退出域后，将域用户从administrators组中移除
rmgroupmember 域用户

function RmGroupMember($member){
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type.member => $member"
    }
    Else
    {
        Remove-LocalGroupMember -Group "Administrators" -Member $member
    }
}

# dhcp2static dhcp转成静态ip，和原来的ip和dns一致
dhcp2static

function getnet{
    $wmi = Get-WmiObject win32_networkadapterconfiguration -Filter "ipenabled = 'true'"
    $isDhcp = $wmi.DHCPEnabled
    $ip = $wmi.IPAddress -notlike "*:*"
    $netmask = $wmi.IPSubnet[0]
    $gateway = $wmi.DefaultIPGateway
    $dnsservers = $wmi.DNSServerSearchOrder
    return $ip ,$netmask ,$gateway ,$dnsservers
}

function Dhcp2Static{
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type."
    }
    Else
    {
        $wmi = Get-WmiObject win32_networkadapterconfiguration -Filter "ipenabled = 'true'"
        $isDhcp = $wmi.DHCPEnabled
        $ip = $wmi.IPAddress -notlike "*:*"
        $netmask = $wmi.IPSubnet[0]
        $gateway = $wmi.DefaultIPGateway
        $dnsservers = $wmi.DNSServerSearchOrder
        $wmi.EnableStatic($ip,$netmask)
        $wmi.SetGateways($gateway)
        $wmi.SetDNSServerSearchOrder($dnsservers)
    }
}

# setstatic 设置静态ip 需接受4或5个参数 dns2不是必须的，如果只是一个dns，就只要接收到dns1
setstatic ip netmask gateway dns1 dns2

function SetStaticNet($ip,$netmask,$gateway,$dns1,$dns2="none"){
    If ($whatif)
    {
        Write-Host "WhatIf: Use function $type . ip => $ip, netmask => $netmask, gateway => $gateway, dns1 => $dns1, dns2 => $dns2."
    }
    Else
    {
        $wmi = Get-WmiObject win32_networkadapterconfiguration -Filter "ipenabled = 'true'"
        if ($dns2 -eq "none"){
            $dnsservers = "$dns1"
        }
        Else
        {
            $dnsservers = $dns1,$dns2
        }
        $wmi.EnableStatic($ip,$netmask)
        $wmi.SetGateways($gateway)
        $wmi.SetDNSServerSearchOrder($dnsservers)
    }
}

# 启用User Profile Disk方式得桌面，客户端执行如下命令
# enupd share_path
# share_path 是共享路径，使用 \\SERVER\SHARE\Profiles\Win10-1607
# 实例 (gwmi -ns root\cimv2\terminalservices -class win32_tssessiondirectory).enableuservhd("\\SERVER\SHARE\Profiles\Win10-1607","<UvhdRoamingPolicy><RoamingMode>0</RoamingMode></UvhdRoamingPolicy>")
# 注册表里查看下面得位置是否生效，关键字"UvhdEnabled" 
# HKLM\System\CurrentControlSet\Control\Terminal Server\ClusterSettings

function EnUPD($share_path){
    (gwmi -ns root\cimv2\terminalservices -class win32_tssessiondirectory).enableuservhd("$share_path","<UvhdRoamingPolicy><RoamingMode>0</RoamingMode></UvhdRoamingPolicy>")
}